package top.zhimingw.controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import top.zhimingw.dto.ResponseResult;
import top.zhimingw.elasticsearch.ArticleRespository;
import top.zhimingw.pojo.Admin;
import top.zhimingw.pojo.Front;
import top.zhimingw.redis.RedisService;
import top.zhimingw.service.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @ClassName: BaseController
 * @Description: 控制器的基类，所有控制器必须继承此类
 * @Author:zhimingw
 * @Version 1.0
 * @since 2021/5/30 20:27
 */
public class BaseController {

    @Autowired
    public AdminService adminService;
    @Autowired
    public ArticleService articleService;
    @Autowired
    public KindService kindService;
    @Autowired
    public TagService tagService;
    @Autowired
    public FrontService frontService;
    @Autowired
    public MenuService menuService;
    @Autowired
    public LinkService linkService;
    @Autowired
    CommentService commentService;
    @Autowired
    public StatisticsService statisticsService;
    @Autowired
    public RedisService redisService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    ArticleRespository articleRespository;


    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected HttpSession session;


    protected ResponseResult result;//自定义ResponseResult在controller响应信息主体和自定义全局及局部异常中的实现
    protected Admin loginAdmin;
    protected Admin user;
    protected Front front;

    /**
     * 在每个子类方法调用之前先调用，设置request,response,session这三个对象
     */
    @ModelAttribute
    public void setReqAndRes(HttpServletRequest request,HttpServletResponse response){
        this.request = request;
        this.response = response;
        this.session = request.getSession();
        result = new ResponseResult();
        //设置admin对象
        loginAdmin = (Admin) session.getAttribute("admin");
        System.out.println("loginAdmin=>"+loginAdmin);
        //获取前台主页对象
        front = frontService.queryById(1);
        //获取用户对象
        user = adminService.queryById(1);
        if(user!=null){
            user.setPassword(null);
            user.setRecentLogin(null);
            user.setOpenId(null);
            user.setSalt(null);
        }
    }
}
